package abq2.jic.jic.plugin.other.other.mob.mob;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.events.BeforeCreateListDataProviderArgs;
import kd.bos.list.plugin.AbstractMobListPlugin;
import kd.bos.mvc.list.ListDataProvider;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

public class BookMobListPlugin extends AbstractMobListPlugin {
    // 监听工具栏的点击事件
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addItemClickListeners("toolbarap");    // 监听工具栏
    }

    @Override
    public void beforeCreateListDataProvider(BeforeCreateListDataProviderArgs args) {
        args.setListDataProvider(new BookMobListDataProvider());
    }

}

class BookMobListDataProvider extends ListDataProvider {

    /**
     * 加载列表数据
     * @remark
     * 获取系统自动加载的列表数据，然后对内容进行修正
     */
    @Override
    public DynamicObjectCollection getData(int arg0, int arg1) {
        DynamicObjectCollection collection = new DynamicObjectCollection();

        DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load("kdec_book",
                "id, kdec_picture, name, kdec_author, kdec_publisher, kdec_remain_num, kdec_detail, kdec_detail.kdec_bd_book",
                null);
        for (DynamicObject bookObject : dynamicObjects) {
            DynamicObjectCollection detail = bookObject.getDynamicObjectCollection("kdec_detail");
            // 统计可借数量
            int count = 0;
            for (int i = 0; i < detail.size(); i++) {
                if (detail.get(i).get("kdec_bd_book.kdec_billstatusfield").equals("A")) {
                    count++;
                }
                if (i >= detail.size() - 1) {
                    bookObject.set("kdec_remain_num", count);
                }
            }
            collection.add(bookObject);
        }

        return collection;

    }
}